package com.starbaba.core.utils;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.starbaba.core.web.constant.Constants;

/**
 * 应用ehcache管理器
 *
 * @Copyright: Copyright (c) 2006
 * @Company: Revenco Tech Ltd.
 * @anthor chenminhui<chenminhui@revenco.com>
 * @since 2013-10-25 下午02:37:55
 * @version V1.0
 */
public class EhcacheManager {
	
	private static Logger logger = LoggerFactory.getLogger(EhcacheManager.class);
	
	/**
	 * 获取spring配置的ehcache管理器
	 * @return CacheManager
	 *
	 * @Copyright: Copyright (c) 2006
	 * @Company: Revenco Tech Ltd.
	 * @anthor chenminhui<chenminhui@revenco.com>
	 * @since 2013-10-25 下午02:44:51
	 * @version V1.0
	 */
	public static CacheManager getEhcacheManager() {
		return SpringContextManager.getBean("ehcacheManager");
	}
	
	/**
	 * 获取指定名字的缓存
	 * @param cacheName 缓存名称
	 * @return Cache
	 *
	 * @Copyright: Copyright (c) 2006
	 * @Company: Revenco Tech Ltd.
	 * @anthor chenminhui<chenminhui@revenco.com>
	 * @since 2013-10-25 下午05:29:06
	 * @version V1.0
	 */
	public static Cache getCache(String cacheName) {
		return getEhcacheManager().getCache(cacheName);
	}
	
	/**
	 * 获取用于替换session管理的cache
	 * @return Cache
	 *
	 * @Copyright: Copyright (c) 2006
	 * @Company: Revenco Tech Ltd.
	 * @anthor chenminhui<chenminhui@revenco.com>
	 * @since 2013-10-25 下午02:46:32
	 * @version V1.0
	 */
	public static Cache getCacheSession() {
		return SpringContextManager.getBean("memCacheSession");
	}
	
	/**
	 * 从memCacheSession获取指定key的值
	 * @param key 
	 * @return Object
	 *
	 * @Copyright: Copyright (c) 2006
	 * @Company: Revenco Tech Ltd.
	 * @anthor chenminhui<chenminhui@revenco.com>
	 * @since 2013-10-25 下午03:10:41
	 * @version V1.0
	 */
	public static Object getByCacheSession(String key) {
		Element ele = getCacheSession().get(key);
		if(ele == null) {
			logger.warn("找不到缓存[key=" + key + "]");
			return null;
		} else {
			return ele.getObjectValue();
		}
	}
	
	/**
	 * 设置memCacheSession中指定key的值
	 * @param key
	 * @param value
	 *
	 * @Copyright: Copyright (c) 2006
	 * @Company: Revenco Tech Ltd.
	 * @anthor chenminhui<chenminhui@revenco.com>
	 * @since 2013-10-25 下午03:11:10
	 * @version V1.0
	 */
	public static void setByCacheSession(String key, Object value) {
		if(getCacheSession().isKeyInCache(key)) {
			logger.warn("将替换缓存[key=" + key + "]的值");
		}
		
		getCacheSession().put(new Element(key, value));
	}
	
	/**
	 * 在memCacheSession中清除指定key的值
	 * @param key
	 *
	 * @Copyright: Copyright (c) 2006
	 * @Company: Revenco Tech Ltd.
	 * @anthor chenminhui<chenminhui@revenco.com>
	 * @since 2013-10-25 下午03:24:18
	 * @version V1.0
	 */
	public static void removeByCacheSession(String key) {
		logger.info("清除缓存[key=" + key + "]的值");
		getCacheSession().remove(key);
	}
	
	/**
	 * 获取本地磁盘的cache
	 * @return Cache
	 *
	 * @Copyright: Copyright (c) 2006
	 * @Company: Revenco Tech Ltd.
	 * @anthor chenminhui<chenminhui@revenco.com>
	 * @since 2013-10-25 下午02:46:32
	 * @version V1.0
	 */
	public static Cache getDiskCache() {
		return getCache(Constants.CACHE_NAME_DISK_CACHE_COMMON);
	}

}
